package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarIdleParking;
import com.navinfo.tripanalysis.common.arithmetic.enums.EventStatus;
import com.navinfo.tripanalysis.common.config.ConfigMap;
import com.navinfo.tripanalysis.common.protocol.java.EventDataPb;

/**
 * 怠速算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 赋值{@link CommonData#parkingIdle}是否为停车怠速状态<br/>
 * 输出：统计、事件<br/>
 * @author 沈东生
 */
public class IdleParking extends AbstractArithmetic<VarIdleParking> {
    /**
     * 事件类型
     */
    private static final EventDataPb.EventType EVENT_TYPE = EventDataPb.EventType.vehicleParkingIdle;
    /**
     * 怠速算法 转速阀值 单位：rpm
     */
    private long rotationThreshold = ConfigMap.getInteger("ParkingIdle_Rotation_Threshold");

    /**
     * 判断当前点是否为怠速状态
     */
    public boolean isCurParkingIdle() {
        CommonData data = getCommonData();
        // 怠速状态判定：引擎转速>500 and 车速==0
        return (data.getRotation() > rotationThreshold && data.getSpeedKmh() == 0);
    }

    @Override
    public void calculate() {
        logger.debug("进入怠速算法.[terminalId:{}]", terminalId);
        VarIdleParking var = getObj();

        boolean previousIdle = var.isPreParkingIdle();
        boolean currentIdle = isCurParkingIdle();
        var.setPreParkingIdle(currentIdle);
        logger.debug("怠速算法，怠速状态：previousIdle:{},currentIdle:{}.[terminalId:{}]", previousIdle, currentIdle, terminalId);

        // 公共参数中设置此值，手刹算法要依赖此值
        getCommonData().setParkingIdle(currentIdle);

        //算法计算
        EventStatus eventStatus = super.getEventStatus(previousIdle, currentIdle);
        switch (eventStatus) {
            case START:
                logger.debug("怠速算法，事件开始.[terminalId:{}]", terminalId);
                startEventData(EVENT_TYPE);
                var.setNumber(var.getNumber()+1);
                break;
            case IN:
                logger.debug("怠速算法，事件中.[terminalId:{}]", terminalId);
                calcVarData();
                calcEventData(EVENT_TYPE);
                break;
            case END:
                logger.debug("怠速算法，事件结束.[terminalId:{}]", terminalId);
                calcVarData();
                calcEventData(EVENT_TYPE);
                endEventData(EVENT_TYPE);
                break;
            default:
                logger.debug("怠速算法，怠速状态：上一次和本次都未怠速，跳出算法.[terminalId:{}]", terminalId);
        }

        logger.debug("怠速算法结束.[terminalId:{}]", terminalId);
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarIdleParking var = getObj();
        statisticDataOut.setIdleNumber(var.getNumber());
        statisticDataOut.setIdleFuel(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setIdleDuration(ArithmeticUtils.timeMsToSecond(var.getTimeMs()));
    }

    /**
     * 累加统计的数据
     */
    private void calcVarData() {
        VarIdleParking var = getObj();
        CommonData commonData = getCommonData();
        var.setFuel(var.getFuel()+ commonData.getDffOil());
        var.setTimeMs(var.getTimeMs() + commonData.getDffTime());
    }
}
